﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Service.Model;
using System.Data;
using GtsServer.DAL;
using GtsServer.Model;
using GtsServer.Common;

namespace GtsServer.Service.Controller
{
    public class GameMainController
    {
        public GameMainController() { }

        public GameMainResponse GetGameTaskMatch(GameMatchRequest request = null)
        {
            GameMainResponse response = new GameMainResponse();
            DataSet ds = new DataSet();
            //累加比赛积分 及比赛币
            if (request != null)
            {
                string userAccount = BaseController.Account(request.info);

                if (string.IsNullOrEmpty(userAccount))
                    return response;

                TaskDAL taskDal = new TaskDAL();
                try
                {
                    ds = taskDal.EditMatchScoreAndGold(userAccount, request.match.id, request.match.total_bonus, request.match.match_coin, request.match.first_coin, request.match.start);
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog("更新比赛完成！ok!", ex);
                }

            }

            response.odds = new ChanceResponse();
            response.odds.model = new List<ChanceModelManage>();
            response.odds.little_pic = new List<List<int>>();
            response.odds.lucky_pic = new List<List<int>>();
            response.odds.start = new List<List<int>>();
            response.odds.end = new List<List<int>>();
            response.odds.sel_high = new List<SelHighManage>();
            response.odds.sel_low = new List<SelLowManage>();
            response.odds.lucky_light = new List<LuckyLightManage>();
            response.odds.cat = new List<CatManage>();
            response.odds.sea = new List<List<int>>();
            response.odds.bar = new List<List<int>>();
            response.odds.plane = new List<CatManage>();
            response.notice = new NoticeGameMainManage();
            response.notice.detail = new List<NoticeGameMainDetailManage>();
            response.status = 0;
            response.match = new GameMainMatchManage();
            response.cheat = 1;

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                //黑名单
                response.cheat = Convert.ToInt32(ds.Tables[0].Rows[0]["state"]);
                response.status = Convert.ToInt32(ds.Tables[0].Rows[0]["stutas"].ToString());
                response.match.score = Convert.ToInt32(ds.Tables[0].Rows[0]["score"].ToString());
                response.match.is_change = ds.Tables[0].Rows[0]["isChange"] == null ? false : Convert.ToBoolean(ds.Tables[0].Rows[0]["isChange"].ToString());
                response.match.match_gold = Convert.ToInt32(ds.Tables[0].Rows[0]["gold"].ToString());
                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    if (item["Name"].ToString().Equals("chance") && !item["EditTime"].ToString().Equals(request.match.odds_id))
                    {
                        ChanceController chanceController = new ChanceController();
                        try
                        {
                            response.odds = chanceController.GetChance();
                        }
                        catch (Exception ex)
                        {

                            LogHelper.WriteLog("获取概率！ok!", ex);
                        }
                    }
                    else if ((item["Name"].ToString().Equals("notice") && !item["EditTime"].ToString().Equals(request.match.notice_id)))
                    {
                        NoticeDAL dal = new NoticeDAL();
                        try
                        {
                            ds = dal.GetListByTaskIDAndUsed(request.match.id, Convert.ToInt32(EnumNoticeType.比赛公告));
                            foreach (DataRow notice in ds.Tables[0].Rows)
                            {
                                response.notice.id = item["EditTime"].ToString();
                                NoticeGameMainDetailManage model = new NoticeGameMainDetailManage()
                                {
                                    chars = notice["NoticeContent"].ToString(),
                                    color = new NoticeGameMainColorManage()
                                    {
                                        r = Convert.ToInt32(notice["R"].ToString()),
                                        g = Convert.ToInt32(notice["G"].ToString()),
                                        b = Convert.ToInt32(notice["B"].ToString()),
                                    }
                                };
                                response.notice.detail.Add(model);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.WriteLog("获取公告！ok!", ex);
                        }
                    }
                }
            }
            return response;
        }

        public GameMainResponse GetGameMain(GameMainRequest request = null)
        {
            GameMainResponse response = new GameMainResponse();
            response.odds = new ChanceResponse();
            response.odds.model = new List<ChanceModelManage>();
            response.odds.little_pic = new List<List<int>>();
            response.odds.lucky_pic = new List<List<int>>();
            response.odds.start = new List<List<int>>();
            response.odds.end = new List<List<int>>();
            response.odds.sel_high = new List<SelHighManage>();
            response.odds.sel_low = new List<SelLowManage>();
            response.odds.lucky_light = new List<LuckyLightManage>();
            response.odds.cat = new List<CatManage>();
            response.odds.sea = new List<List<int>>();
            response.odds.bar = new List<List<int>>();
            response.odds.plane = new List<CatManage>();
            response.notice = new NoticeGameMainManage();
            response.notice.detail = new List<NoticeGameMainDetailManage>();
            response.cheat = 1;

            DataSet ds = new DataSet();
            if (request != null)
            {

                string userAccount = BaseController.Account(request.info);

                if (string.IsNullOrEmpty(userAccount))
                    return response;

                UserPrizeDAL upDal = new UserPrizeDAL();
                ds = upDal.InitUserPrize(userAccount, request.classic.first_coin, request.classic.coin);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    response.egg = Convert.ToInt32(ds.Tables[0].Rows[0]["eggCount"].ToString());
                    response.login_bonus = Convert.ToInt32(ds.Tables[0].Rows[0]["loginBonus"].ToString());
                    response.cheat = Convert.ToInt32(ds.Tables[0].Rows[0]["state"]);
                }
                else
                {
                    response.egg = 0;
                    response.login_bonus = 0;
                    response.cheat = 1;
                }
            }

            ChanceController chanceController = new ChanceController();
            response.odds = chanceController.GetChance();

            NoticeDAL dal = new NoticeDAL();

            ds = dal.GetListByTaskIDAndUsed(0, Convert.ToInt32(EnumNoticeType.经典模式公告));
            foreach (DataRow item in ds.Tables[0].Rows)
            {
                //response.notice.id = Convert.ToInt32(item["ID"].ToString());
                NoticeGameMainDetailManage model = new NoticeGameMainDetailManage()
                {
                    chars = item["NoticeContent"].ToString(),
                    color = new NoticeGameMainColorManage()
                    {
                        r = Convert.ToInt32(item["R"].ToString()),
                        g = Convert.ToInt32(item["G"].ToString()),
                        b = Convert.ToInt32(item["B"].ToString()),
                    }
                };
                response.notice.detail.Add(model);
            }
            return response;
        }

    }
}
